package ua.sumdu.LukyanovChalyi.Client;

import java.util.*;
import java.io.*;
import ua.sumdu.LukyanovChalyi.Server.*;
import org.apache.log4j.*;
import org.apache.log4j.xml.*;

public class View {

    private static final Logger logger = Logger.getLogger(View.class);
    private BufferedReader in;

    public View() {
        DOMConfigurator.configure("log4j.xml");
        in = new BufferedReader(new InputStreamReader(System.in));
    }

    public synchronized void showActions() {
        System.out.println("Actions:");
        System.out.println("1. Add book");
        System.out.println("2. Set book's active");
        System.out.println("3. Remove book");
        System.out.println("4. Write books to file");
        System.out.println("5. Read books from file");
        System.out.println("6. Show books");
        System.out.println("7. Exit");
    }

    @SuppressWarnings("finally")
    public Integer getAction() {
        Integer action = null;
        try {
            action = new Integer(in.readLine());
        } catch (NumberFormatException e) {
            this.showException("Must be number", e);
        } finally {
            return action;
        }
    }

    @SuppressWarnings("finally")
    public CopyBook getCopyBook() {
        CopyBook copyBook = null;
        try {
            System.out.print("Authors: ");
            StringTokenizer stoken = new StringTokenizer(in.readLine(), ", ");
            List<String> authors = new LinkedList<String>();
            while (stoken.hasMoreTokens()) {
                authors.add(stoken.nextToken());
            }
            System.out.print("Title: ");
            String title = in.readLine();
            System.out.print("Date (1992): ");
            int date = Integer.parseInt(in.readLine());
            System.out.print("Pages: ");
            int pages = Integer.parseInt(in.readLine());
            Book book = new Book(authors, title, date, pages);
            copyBook = new CopyBook(book, false);
        } catch (NumberFormatException e) {
            this.showException("Must be number", e);
        } finally {
            return copyBook;
        }
    }

    @SuppressWarnings("finally")
    public Boolean getActive() {
        Boolean active = null;
        try {
            System.out.print("Active (yes or no): ");
            String act = in.readLine();
            if (act.equals("yes")) {
                active = true;
            } else if (act.equals("no")) {
                active = false;
            } else {
                throw new IllegalArgumentException("Must be yes or no");
            }
        } catch (IllegalArgumentException e) {
            this.showException("Must be yes or no", e);
        } finally {
            return active;
        }
    }

    @SuppressWarnings("finally")
    public Long getIDCopyBook() {
        Long id = null;
        try {
            System.out.print("Book's id: ");
            id = Long.parseLong(in.readLine());
            if (id <= 0) {
                throw new IllegalArgumentException("ID must be > 0 ");
            }
        } catch (NumberFormatException e) {
            this.showException("Must be number", e);
        } catch (IllegalArgumentException e) {
            this.showMessage(e.getMessage());
        } finally {
            return id;
        }
    }

    @SuppressWarnings("finally")
    public String getFileName() {
        String filename = null;
        try {
            System.out.print("File name: ");
            String file = in.readLine();
            if (file.endsWith(".xml") || file.endsWith(".txt")) {
                filename = file;
            } else {
                throw new IllegalArgumentException("Must be .xml or .txt");
            }
        } catch (IllegalArgumentException e) {
            this.showException("Must be .xml or .txt", e);
        } finally {
            return filename;
        }
    }

    public synchronized void showMessage(String message) {
        System.out.println(message);
    }

    public synchronized void showException(String message, Throwable e) {
        logger.error(message, e);
        System.out.println(message);
    }

    public synchronized void showBooks(List<CopyBook> list) {
        System.out.printf("%-19s%-15s%-22s%-6s%-10s%s%n", "ID", "Title", "Authors", "Date", "Pages", "Active");
        for (CopyBook cb : list) {
            showCopyBook(cb);
        }
    }

    private synchronized void showCopyBook(CopyBook cb) {
        System.out.printf("%-19s%-15s%-22s%-6s%-10s%s%n",
                cb.getID(),
                cb.getBook().getTitle(),
                cb.getBook().getAuthors(),
                cb.getBook().getDate(),
                cb.getBook().getNumberOfPages(),
                cb.isActive());
    }
}